﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class operate_operateCourseInfo : System.Web.UI.Page
{
    string[] keys;
    protected void Page_Load(object sender, EventArgs e)
    {
        keys = Request.QueryString.AllKeys;
        //规范：1.operate 2.table 3.id
        if (Request.QueryString[keys[0]].ToString().Trim() == "del")
        {
            //删除用户
            DelTestQuestion();

        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "add")
        {
            //添加用户

            AddTestQuestion();
        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "update")
        {
            //修改用户
            UpdateTestQuestion();

        }
        Response.End();
    }
    public void UpdateTestQuestion()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_course")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_course set " + s
                + " where course_id ='" + Request.QueryString[keys[2]] + "'";

            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        

    }
    public void AddTestQuestion()
    {

        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_course")
        {

            string sql = "INSERT INTO [dbo].[tb_course]" +
                            "         ([course_id]" +
                            "           ,[course_name]" +
                            "           ,[class_score])" +
                            "     VALUES("+
                        String.Format("'{0}',", Request.QueryString[keys[2]]) +
                        String.Format("'{0}',", Request.QueryString[keys[3]]) +
                        String.Format("'{0}')", Request.QueryString[keys[4]]);
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #endregion

       
    }
    public void DelTestQuestion()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_course")
        {
             string course_id = Request.QueryString[keys[2]];
            //还要删除相关课程记录
             string radioSql = "delete from tb_radio_questions where course_id ='" + course_id + "'";
             int k1 = SqlServer.ExecuteNonQuery(radioSql);
             string mutilSql = "delete from tb_mutil_questions where course_id ='" + course_id + "'";
             int k2 = SqlServer.ExecuteNonQuery(mutilSql);
             string judgeSql = "delete from tb_judge_questions where course_id ='" + course_id + "'";
             int k3 = SqlServer.ExecuteNonQuery(judgeSql);
             string answerSql = "delete from tb_answer_questions where course_id ='" + course_id + "'";
             int k4 = SqlServer.ExecuteNonQuery(answerSql);
             string testSql = "delete from tb_test_info where course_id ='" + course_id + "'";
             int k5 = SqlServer.ExecuteNonQuery(testSql);
            string sql = "delete from tb_course where course_id ='" + course_id + "'";
            int k6 = SqlServer.ExecuteNonQuery(testSql);
            string testQuestionsql = "delete from tb_test_questions where course_id ='" + course_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
     
        }

        
    }
}